/**
 * @file task_time.c
 * @brief demo how to create a task, print time every second
 * @author Tome (zigbee86@163.com)
 * @date 2021-01-05
 */
 

#include "OSAL.h" 
#include "osal_log.h"

#include "task_time.h"
#include "stdio.h"

#include "hal_led.h"


#define TM_TEST_PIN   5
#define TM_TEST_PORT   3
#define TM_TEST_HIGH()    Gpio_SetIO(TM_TEST_PORT, TM_TEST_PIN, 1)
#define TM_TEST_LOW()    Gpio_SetIO(TM_TEST_PORT, TM_TEST_PIN, 0)



#define  TIME_LED_SET(a,b)       //HalLedSet(a,b)

static uint8 test_pin     = 0;


static uint8 Time_TaskID;
//time_t gTime = {0};


//static void time_updata(void);
static void time_print(void);

/**************************************************************************************************
 * @fn      Hal_Init
 *
 * @brief   Hal Initialization function.
 *
 * @param   task_id - Hal TaskId
 *
 * @return  None
 **************************************************************************************************/
void Time_Init( uint8 task_id )
{
  /* Register task ID */
  Time_TaskID = task_id;
    
  UNUSED_VARIABLE(Time_TaskID);
  
  //osal_start_reload_timer(task_id, TASK_TIME_EVT_1S, 1000 );
  osal_start_reload_timer(task_id, TASK_TIME_EVT_PRINT, 1000 );
  //  osal_start_reload_timer(task_id, TASK_TIME_EVT_10S, 10000);
    
    TIME_LED_SET(HAL_LED_1, HAL_LED_MODE_OFF);
    
   // Gpio_InitIO(TM_TEST_PORT, TM_TEST_PIN, GpioDirOut);
    
   //osal_pwrmgr_task_state( task_id, PWRMGR_HOLD);
    
  LOG("Time task start");
  
}


void test_pin_toggle(void)
{
      if ( test_pin ) TM_TEST_HIGH();
      else
          TM_TEST_LOW();
      
      test_pin = !test_pin;
}


/**************************************************************************************************
 * @fn      Time_ProcessEvent
 *
 * @brief   Time Process Event
 *
 * @param   task_id - Hal TaskId
 *          events - events
 *
 * @return  None
 **************************************************************************************************/
uint16 Time_ProcessEvent( uint8 task_id, uint16 events )
{
  uint8 *msgPtr;
  
  (void)task_id;  // Intentionally unreferenced parameter

  if ( events & SYS_EVENT_MSG )
  {
        msgPtr = osal_msg_receive(task_id);

        while (msgPtr)
        {
          /* Do something here - for now, just deallocate the msg and move on */
            
          //WARN("%s",msgPtr);

          /* De-allocate */
          osal_msg_deallocate( msgPtr );
          /* Next */
          msgPtr = osal_msg_receive( task_id );
        }
      return (events ^ SYS_EVENT_MSG );
  }
  
  
  if ( events & TASK_TIME_EVT_PRINT )
  {
      time_print();
      
      //TIME_LED_SET(HAL_LED_1, HAL_LED_MODE_TOGGLE);
      
      return ( events ^ TASK_TIME_EVT_PRINT );
  }
  
  if ( events & TASK_TIME_EVT_10S )
  {
      TIME_LED_SET(HAL_LED_1, HAL_LED_MODE_TOGGLE);
      
      return (events ^ TASK_TIME_EVT_10S);
  }
  
  
	//  all unhandl event would be clear 
   return 0;
}



// time print function
static void time_print(void)
{
    //osal_ConvertUTCTime( UTCTimeStruct *tm, UTCTime secTime )
    UTCTime time = osal_getClock();
    
    UTCTimeStruct utc;
    
    osal_ConvertUTCTime(&utc, time);

    //LOG("%02d:%02d:%02d", gTime.hour, gTime.min, gTime.sec);
    //WARN("%02d:%02d:%02d", gTime.hour, gTime.min, gTime.sec);
    WARN("%02d:%02d:%02d", utc.hour, utc.minutes, utc.seconds);
    //ERR("TIME  %2d:%2d:%2d", gTime.hour, gTime.min, gTime.sec);
}
